<template>
  <el-descriptions title="基本信息" :column="3" border>
    <el-descriptions-item v-for="row in info_data" :label="row.label" >{{ row.value }}</el-descriptions-item>
  </el-descriptions>

  <h2>债务信息</h2>

  <el-descriptions v-for="(debt,i) in debt_list" :column="6" border :title="'债务数据-'+i" direction="vertical">
    <el-descriptions-item label="债务机构" width="180" >
      {{debt.debt_company_parent_title}} - {{debt.debt_company_title}}
    </el-descriptions-item>

    <el-descriptions-item label="债务数据" min-width="300" >
      <el-form :inline="true" style="width: 100%;text-align: left;">
        <template v-for="row in debt.extend">
          <el-form-item v-if="row.type!='hidden'" :label="row.label+':'">
            <component :is="row.type" name="" :value="row.value" v-bind="bind_param(row)" :is_info="true" :is_list="true"></component>
          </el-form-item>
        </template>
      </el-form>
    </el-descriptions-item>

    <el-descriptions-item label="处理意见" min-width="180" >
      <template #default>
        <text_more v-model:value="debt.note_system" @change="debt_change(debt, 'note_system', $event)"></text_more>
      </template>
    </el-descriptions-item>

    <el-descriptions-item label="金融顾问建议" min-width="180" >
      <template #default>
        <text_more v-model:value="debt.note_admin" @change="debt_change(debt, 'note_admin', $event)"></text_more>
      </template>
    </el-descriptions-item>

    <el-descriptions-item label="委托项目" width="120" >
      <template #default>
        <div>
          <el-button @click="select_link(debt)">选择项目</el-button>
        </div>
        <div v-if="debt.tysd_store.id>0">
          <a :href="debt.tysd_store.href" target="_blank">{{debt.tysd_store.store_name}}<br>（{{debt.tysd_store.price}}）</a>
        </div>
      </template>
    </el-descriptions-item>

    <el-descriptions-item label="状态" width="120" >
      <template #default>
        <div v-if="debt.status=='need_extend'">
          <el-tag type="danger">待补充资料</el-tag>
        </div>
        <div v-else>
          <el-button @click="add_extend(debt)">重新提交</el-button>
        </div>
      </template>
    </el-descriptions-item>

    <el-descriptions-item label="资料补充记录" width="100%" span="6" v-if="debt.debt_extend_list.length" >
      <list :cols="debt_extend_list_cols" :data="debt.debt_extend_list"></list>
    </el-descriptions-item>

  </el-descriptions>

  <forms ref="forms" :config="config" @submit:forms="onsubmit" :btns="btns">

  </forms>

</template>

<script>
import { ElMessageBox } from 'element-plus'
import {iframe} from "@/js/tool.js";
import {ajax} from "@/js/api_tool.js";
export default {
  components: {},

  props: ['__iframe'],
  data(){
    let id = this.$get('id'), btns = undefined;
    let model = {name:this.$get('model_name'), label:this.$get('model_label','')};
    let action = this.$get('action',id>0?'edit':'add');
    let controller = this.camel_str(model.name);
    return {
      id: id,
      btns: btns,
      config: {},
      model: model,
      action: action,
      action_map: {},

      info_data: [],
      debt_list: [],

      note_admin_config: {},

      debt_extend_list_cols: [
        {name:'title', label:'补充说明',type:"text_more"},
        {name:'head_img', label:'补充资料',type:"file_imgs",download:1},
        {name:'create_time', label:'需求时间', width:'165px'},
        {name:'edit_time', label:'补充时间', width:'165px'},
      ],
    }
  },
  methods: {
    add_extend(row){
      ElMessageBox.prompt('输入资料补充备注', '重新提交资料', {
        inputType: 'textarea',
        confirmButtonText: '提交',
        cancelButtonText: '取消',
        inputValidator: function (v){
          if (!v) return '备注必填';
          return true;
        },
      }).then((data)=>{
        let url = `/admin/debt_extend/add?debt_id=${row.id}`;
        ajax({url:url, method:'POST', data:{title:data.value}}, (res)=>{
          this.tips("操作成功",1);
          row.status = "need_extend";
          return false;
        });
      }).catch(() => {})
    },
    select_link(row){
      let app = this;
      this.iframe_select('/index/other_list?name=tysd_store', function (selected){
        if (!selected.length) return ;
        if (selected.length > 1) return app.$message.error('只能选择一个');
        let data = selected[0];
        data.href = "http://edu.taiyushangdao.com/wap/store/detail.html?id="+data.id;
        data.title = data.store_name;
        app.debt_change(row, "tysd_store", JSON.stringify(data));
        row.tysd_store = data;
        return true;
      })
    },
    debt_change(row, key, v) {
      let url = `/admin/debt/edit?id=${row.id}`;
      ajax({url:url, method:'POST', data:{[key]:v}}, (res)=>{return false;});
    },
    url(init){
      let param = {};
      if (init) param._is_init_ = 7;
      if (this.id) param.id = this.id;
      let param_str = s94Mjs.param_encode(param);
      return `/admin/${this.model.name}/${this.action}?${param_str}`
    },
    param(data){
      data = data || {};
      data.model_name = this.model.name;
      // data.model_label = this.model.label;
      return s94Mjs.param_encode(data);
    },
    onsubmit(formData){
      ajax({url:this.url(), method:'POST', data:formData}, (res)=>{
        if (this.__iframe){
          iframe.exe('model_v2_base/index?'+this.param(), '$refs.list_url.list');
          iframe.hide('model_v2_base/debt_consult_order_info?'+this.param());
          if (!this.id) this.$refs['forms'].init();
        }else {
          this.$router.push('index?'+this.param());
        }
      }, (res)=>{
        if (res.errors) this.$refs['forms'].errors = res.errors;
      })
    },
    change_after_init(){
      let param = {};
      this.change_need_init.forEach((k)=>{
        param[k] = this.config[k].value;
      })
      this.init(param);
    },
    init(param){
      let is_first = !param;
      let is_info = this.$get('is_info');
      ajax({url:this.url(true), data:param}, (res)=>{
        if (is_first){
          this.model = res.model;
          this.action_map = res.action_map;
          if (is_info) this.btns = [];
        }
        //基本信息
        let info_data_ks = ["user_nickname","user_phone","create_time","order_no"];
        let data_map = {};
        this.info_data = [];
        res.data.forEach((v)=>{
          if (~info_data_ks.indexOf(v.name)) this.info_data.push(v);
          if (v.can_edit == 1){
            this.config[v.name] = v;
          }
          data_map[v.name] = v;
        })
        //债务数据列表
        let debt_info_list = data_map.debt_info_all_extend_list?.value || [];
        this.debt_list = [];
        let ks = ["debt_company_title", "debt_company_parent_title", "note_system","note_admin", "id", "tysd_store", "link_link_info", "status", "debt_extend_list"];
        debt_info_list.forEach((debt_init)=>{
          let row = {extend:[]};
          debt_init.forEach(v=>{
            let k = v.name;
            if (~ks.indexOf(k)) {
              row[k] = v.value;
            }else {
              row.extend.push(v);
            }
          })
          this.debt_list.push(row);
        })
        this.debt_list.sort((a,b)=>{return a.debt_company_parent_title>b.debt_company_parent_title?1:-1;})

        return false;
      })
    },

  },
  computed: {
    change_need_init(){
      let res = [];
      let this_action = this.action_map[this.action];
      if (!this_action) return res;
      res = this_action.init_need_param ? s94Mjs.to_string(this_action.init_need_param).split(",") : [];
      return res;
    }
  },
  mounted() {
    let app = this;
    if (!this.__iframe) window['app'] = this;
    if (!app.model.name){
      return this.__iframe ? iframe.hide('debt_consult_order_info') : this.$router.back();
    }
    //其他初始化操作
    this.init();
  }
}
</script>
